from pymongo import MongoClient
from log import MyLogging

log = MyLogging()


class Mongo(object):
    def __init__(self):
        self.client = MongoClient(host='127.0.0.1', port=27017)
        self.db = self.client['blibli']
        self.duplicate_removal = self.db['duplicate_removal']
        self.kuaishou_id = self.db['kuaishou_id']
        self.id_and_title = self.db['id_and_title']

    def get_kusishou_ids(self):
        ids = {}
        iiiii = [y for y in self.kuaishou_id.find()]
        iiiii.reverse()
        for i in iiiii:
        # for i in self.kuaishou_id.find():
            ids[i['_id']] = {
                'tag': i['tag'],
                'tid': i['tid'],
                'platform': i['platform'],
            }
        return ids

    def set_data(self, data):
        try:
            data['_id'] = data["hid"]
            del data["hid"]
            self.duplicate_removal.insert_one(
                data
            )
            log.write_logger(f'{data["_id"]} 已写入mongo')
        except Exception as e:
            log.write_logger(str(e))

    def get_title(self, title):
        if self.id_and_title.find_one({'title': title}):
            return True
        return False

    def get_data(self, _id, title):
        if self.id_and_title.find_one({'_id': _id}):

            return True
        else:
            self.id_and_title.insert_one({
                '_id': _id,
                'title': title,
            })
            return False


if __name__ == '__main__':
    M = Mongo()
    # for i in M.id_and_title.find():
    #     print(i)
    new = [
        {'_id': '3xm83xh233zyy49', 'tag': '快手女神,搞笑,不 潘', 'tid': 21, 'platform': '快手'},
        #{'_id': '3x27v9qamkhv25m', 'tag': '快手女神,搞笑,咬米', 'tid': 21, 'platform': '快手'},
        #{'_id': '3xscdigrbdg8yb4', 'tag': '快手女神,性感,白泽又胖了', 'tid': 21, 'platform': '快手'},
        #{'_id': '3xm55xedbwtrc76', 'tag': '快手女神,cosplay,点点很乖', 'tid': 21, 'platform': '快手'},
        #{'_id': '3xq9pthpdh67gey', 'tag': '快手女神,cosplay,抱走果果', 'tid': 21, 'platform': '快手'},
        #{'_id': '3xvzpp6fgf5rja6', 'tag': '快手女神,变装,刀小刀', 'tid': 21, 'platform': '快手'},
        #{'_id': '3xe3eierxcszzam', 'tag': '爱情短片,撩妹,七鹤大人', 'tid': 21, 'platform': '快手'},
        #{'_id': '3xtrw8xqb697re2', 'tag': '穷游,张飞,潮汕阿秋', 'tid': 21, 'platform': '快手'},
    ]
    for i in new:
        M.kuaishou_id.insert_one(
            i
        )
    # for i in M.duplicate_removal.find():
    #     print(i)
